export const dateTypeFormat = function(fmt, date, FEtag) {
	if (!(date instanceof Date)) {
		throw new Error('Invalid date object');
	}
	let ret
	const opt = {
		'Y+': date.getFullYear().toString(), // 年
		'm+': (date.getMonth() + 1).toString(), // 月
		'd+': date.getDate().toString(), // 日
		'H+': date.getHours().toString(), // 时
		'M+': date.getMinutes().toString(), // 分
		'S+': date.getSeconds().toString() // 秒
		// 有其他格式化字符需求可以继续添加，必须转化成字符串
	}
	if (FEtag == "end") {
		let endDay = new Date(date.getFullYear(), (date.getMonth() + 1).toString(), 0)
		opt["d+"] = (endDay.getDate()).toString()
	} else if (FEtag == "first") {
		opt["d+"] = "1"
	}
	for (const k in opt) {
		ret = new RegExp('(' + k + ')').exec(fmt)
		if (ret) {
			fmt = fmt.replace(ret[1], (ret[1].length === 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, '0')))
		}
	}
	return fmt
}
// 数字格式化
export const takeBigNumber = (val) => {
  const num = parseFloat(val)
  if (isNaN(num)) return '0.00'
  
  const units = [
    { value: 1e8, symbol: 'E' },
    { value: 1e4, symbol: 'W' }
  ]
  
  const unit = units.find(u => Math.abs(num) >= u.value)
  return unit 
    ? `${(num / unit.value).toFixed(2)}...${unit.symbol}`
    : num.toFixed(2)
}
export const takeClassList = (arr, takeClass) => {
	if (typeof takeClass == "string") {
		let keyName = takeClass
		takeClass = (item) => item[keyName]
	}
	let outer = {}
	for (let item of arr) {
		const key = takeClass(item)
		if (!outer[key]) outer[key] = []
		outer[key].push(item)
	}
	return outer
}
export const handleLoad = (message) => {
	uni.reLaunch({
		url: "/pages/index/index"
	});
	myShowToast(message);
}

export const reLoading = () => {
	var pages = getCurrentPages();
	var page = (pages[pages.length - 1]).route;
	uni.reLaunch({ //关闭所有页面，页面 good
		url: "/" + page
	})
}
// 节流函数
export const throttle = (fn, delay = 1000) => {
  let timer = null
  return (...args) => {
    if (timer) {
      clearTimeout(timer)
      timer = setTimeout(() => timer = null, delay)
      return
    }
    fn.apply(this, args)
    timer = setTimeout(() => timer = null, delay)
  }
}
// Toast 提示
export const myShowToast = throttle((message, options = {}) => {
  uni.showToast({
    title: message,
    position: 'bottom',
    icon: 'none',
    duration: 1000,
    ...options
  })
})